package com.e9where.canpoint.customrecycler.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/21.
 * adapter的基类
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHold>{
    //数据
    private List<T> list;
    //布局id
    private int layout_id;
    private Context context;

    //构造函数
    public BaseAdapter(List<T> list, int layout_id, Context context) {
        if (list==null)
            list=new ArrayList<>();
        else
            this.list = list;
        this.layout_id = layout_id;
        this.context = context;
    }

    public BaseAdapter(int layout_id, Context context) {
        this.layout_id = layout_id;
        this.context = context;
        list=new ArrayList<>();
    }

    //点击事件
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    //长按监听
    protected OnItemClickLongListener onItemClickLongListener;
    public void setOnItemClickLongListener(OnItemClickLongListener onItemClickLongListener) {
        this.onItemClickLongListener = onItemClickLongListener;
    }

    @Override
    public BaseViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(layout_id, parent, false);
        return new BaseViewHold(inflate);
    }

    @Override
    public void onBindViewHolder(BaseViewHold holder, final int position) {
        if (onItemClickListener!=null)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.OnItemClickCall(position);
                }
            });
        if (onItemClickLongListener!=null)
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemClickLongListener.onItemClickLongCall(position);
                    return false;
                }
            });
        setData(holder,position,list.get(position));
    }

    //设置数据
    protected abstract void setData(BaseViewHold hold,int position,T t);

    @Override
    public int getItemCount() {
        return list.size();
    }

    //刷新数据
    public void flush(List<T> listData)
    {
        if (getItemCount()>0)
            list.clear();
        list.addAll(listData);
        notifyDataSetChanged();
    }

    //添加数据
    public void addAll(List<T> listData)
    {
        int start_itemCount = getItemCount();
        list.addAll(listData);
        notifyItemRangeInserted(start_itemCount,getItemCount());
    }

    public void clear()
    {
        if (list.size()>0)
            list.clear();
        notifyDataSetChanged();
//        int start_itemCount = getItemCount();
//        list.addAll(listData);
//        notifyItemRangeInserted(start_itemCount,getItemCount());
    }

    //删除其中一个positon
    public void removeT(int position)
    {
        list.remove(position);
        notifyItemRemoved(position);
    }

}
